class FreeLogin {
    constructor() {
        this.init();
    }

    init() {
        let flag = true;
        let cookie = $.cookie("TOKEN");
        if (cookie == "null") {
            flag = false;
        } else {
            this.usr = cookie.split("&")[0].split("=")[1];
        }
        console.log(flag);
        let html = this.render(flag);
        $(".topbar-fr").html(html + $(".topbar-fr").html());

        // 退出登录
        flag ? this.dropdown() : "";
    }

    dropdown() {
        $(".dropdownlist").css("display", "inline-block");
        $("#logoutId").click($.proxy(function() {
            $.cookie("TOKEN", null);
            location.reload();
        }, this))
    }

    render(flag) {
        let html = "";
        if (flag) {
            html += `
                    <li class="nav-item" id="topbarLoginUser">
                        <a href="#" class="downmenu"><span class="title" title="${this.usr}">${this.usr}</span><i class="arrow-bt"></i></a>
                        <div class="dropdownlist" style="width: 72px; right: 2px; left: auto; display: none;">
                            <a href="javascript:void(0)" id="logoutId">退出登录</a>
                        </div>
                    </li>
                    `;
        } else {
            html += `
                    <li class="nav-item">
                        <a href="http://localhost:8888/login/login.html"><span class="title" target="_blank">登录</span></a>
                    </li>
                    <li class="nav-item">
                        <a href="http://localhost:8888/login/register.html"><span class="title" target="_blank">注册</span></a>
                    </li>
                    `;
        }
        return html;
    }
}

new FreeLogin();